package com.ruoyi.source.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.source.api.domain.SrcGoStore;
import com.ruoyi.source.api.domain.SrcTask;
import com.ruoyi.source.domain.SrcTaskGoStore;
import com.ruoyi.source.service.ISrcGoStoreService;
import com.ruoyi.source.service.ISrcTaskService;
import com.ruoyi.source.service.ISrcTaskGoStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工作任务Controller
 * 
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/task")
public class SrcTaskController extends BaseController {
    @Autowired
    private ISrcTaskService srcTaskService;
    @Autowired
    private ISrcGoStoreService srcGoStoreService;
    @Autowired
    private ISrcTaskGoStoreService srcTaskGoStoreService;



    /**
     * 查询工作任务列表
     */
    @RequiresPermissions("source:task:list")
    @GetMapping("/list")
    public TableDataInfo list(SrcTask srcTask) {
        startPage();
        List<SrcTask> list = srcTaskService.selectSrcTaskList(srcTask);
        return getDataTable(list);
    }

    /**
     * 导出工作任务列表
     */
    @RequiresPermissions("source:task:export")
    @Log(title = "工作任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrcTask srcTask) {
        List<SrcTask> list = srcTaskService.selectSrcTaskList(srcTask);
        ExcelUtil<SrcTask> util = new ExcelUtil<SrcTask>(SrcTask.class);
        util.exportExcel(response, list, "工作任务数据");
    }

    /**
     * 获取工作任务详细信息
     */
    @RequiresPermissions("source:task:query")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        return success(srcTaskService.selectSrcTaskByTaskId(taskId));
    }

//    /**
//     * 新增工作任务
//     */
//    @RequiresPermissions("source:task:add")
//    @PostMapping
//    @Transactional
//    public AjaxResult add(@RequestBody SrcTask srcTask) {
//        System.out.println(srcTask);
//        srcTask.setCreateTime(DateUtils.getNowDate());
//        srcTask.setCreateBy(SecurityUtils.getUsername());
//        boolean save = srcTaskService.save(srcTask);
//        if (save){
//            ArrayList<SrcGoStore> SrcGoStoreList = new ArrayList<>();
//            for (Map<String, Object> goStoreRecord : srcTask.getGoStoreRecord()) {
//                Object inStoreId = goStoreRecord.get("inStoreId");
//                Object goodsNum = goStoreRecord.get("goodsNum");
//                SrcGoStore srcGoStore = new SrcGoStore();
//                srcGoStore.setInStoreId(Long.valueOf((Integer) inStoreId));
//                srcGoStore.setGoStoreNum(Long.valueOf((Integer) goodsNum));
//                srcGoStore.setCreateBy(SecurityUtils.getUsername());
//                srcGoStore.setCreateTime(new Date());
//                SrcGoStoreList.add(srcGoStore);
//            }
//            save = srcGoStoreService.saveBatch(SrcGoStoreList);
//            if (save){
//                Long taskId = srcTask.getTaskId();
//                Iterable<Long> collect = SrcGoStoreList.stream().map(srcGoStore -> srcGoStore.getGoStoreId()).collect(Collectors.toList());
//                ArrayList<SrcTaskGoStore> SrcTaskGoStoreList = new ArrayList<>();
//                for (Long goStoreId : collect) {
//                    SrcTaskGoStore srcTaskGoStore = new SrcTaskGoStore();
//                    srcTaskGoStore.setTaskId(taskId);
//                    srcTaskGoStore.setGoStoreId(goStoreId);
//                    SrcTaskGoStoreList.add(srcTaskGoStore);
//                }
//                save = srcTaskGoStoreService.saveBatch(SrcTaskGoStoreList);
//            }
//        }
//        return toAjax(true);
//    }

    /**
     * 修改工作任务
     */
    @RequiresPermissions("source:task:edit")
    @Log(title = "工作任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcTask srcTask)
    {
        return toAjax(srcTaskService.updateSrcTask(srcTask));
    }

    /**
     * 删除工作任务
     */
    @RequiresPermissions("source:task:remove")
    @Log(title = "工作任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return toAjax(srcTaskService.deleteSrcTaskByTaskIds(taskIds));
    }
}
